package com.tengits.react.amap3d.maps;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.tengits.react.amap3d.AMapUtils;
import com.amap.api.maps.AMap;
import com.amap.api.maps.model.*;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.views.view.ReactViewGroup;

import java.util.HashMap;
import java.util.Map;


class AMapMarker extends ReactViewGroup implements AMapOverlay {
    private static Map<String, Float> COLORS = new HashMap<String, Float>(){{
      put("AZURE", BitmapDescriptorFactory.HUE_AZURE);
      put("BLUE", BitmapDescriptorFactory.HUE_BLUE);
      put("CYAN", BitmapDescriptorFactory.HUE_CYAN);
      put("GREEN", BitmapDescriptorFactory.HUE_GREEN);
      put("MAGENTA", BitmapDescriptorFactory.HUE_MAGENTA);
      put("ORANGE", BitmapDescriptorFactory.HUE_ORANGE);
      put("RED", BitmapDescriptorFactory.HUE_RED);
      put("ROSE", BitmapDescriptorFactory.HUE_ROSE);
      put("VIOLET", BitmapDescriptorFactory.HUE_VIOLET);
      put("YELLOW", BitmapDescriptorFactory.HUE_YELLOW);
    }};

    private View icon = null;
    private BitmapDescriptor bitmapDescriptor = null;
    private Float anchorU = 0.5f;
    private Float anchorV = 1f;
    public Marker marker = null;
    public LatLng position = null;
    public Float zIndex = 0.0f;
    public String title = "";
    public String snippet = "";
    public Boolean flat = false;
    public Float opacity = 1f;
    public Boolean draggable = false;
    public Boolean clickDisabled = false;
    public Boolean infoWindowDisabled = false;
    public Boolean active = false;

    public AMapMarker(Context context) {
      super(context);
    }

    void setPosition(LatLng p) {
        position = p;
        if(marker != null) {
          marker.setPosition(p);
        }
    }

    void setZIndex(Float value) {
      zIndex = value;
      if(marker != null) {
        marker.setZIndex(value);
      }
    }

    void setTitle(String value) {
        title = value;
        if(marker != null) {
          marker.setTitle(value);
        }
    }

    void setSnippet(String value) {
      snippet = value;
      if(marker != null) {
        marker.setSnippet(value);
      }
    }

    void setFlat(Boolean value) {
      flat = value;
      if(marker != null) {
        marker.setFlat(value);
      }
    }

    void setOpacity(Float value) {
      opacity = value;
      if(marker != null) {
        marker.setAlpha(value);
      }
    }

    void setDraggable(Boolean value) {
      draggable = value;
      if(marker != null) {
        marker.setDraggable(value);
      }
    }

    void setClickDisabled(Boolean value) {
      clickDisabled = value;
      if(marker != null) {
        marker.setClickable(!value);
      }
    }

    void setInfoWindowDisabled(Boolean value) {
      infoWindowDisabled = value;
      if(marker != null) {
        marker.setInfoWindowEnable(!value);
      }
    }

    void setActive(Boolean value) {
      active = value;
      if(marker != null) {
        if (value) {
          marker.showInfoWindow();
        } else {
          marker.hideInfoWindow();
        }
      }
    }

    @Override
    public void addView(View child, int index) {
          super.addView(child, index);
          icon = child;
          icon.addOnLayoutChangeListener(new OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
              updateIcon();
            }
          });
    }

    @Override
    public void add(AMap map) {
      marker = map.addMarker(new MarkerOptions()
              .setFlat(flat)
              .icon(bitmapDescriptor)
              .alpha(opacity)
              .draggable(draggable)
              .position(position)
              .anchor(anchorU, anchorV)
              .infoWindowEnable(!infoWindowDisabled)
              .title(title)
              .snippet(snippet)
              .zIndex(zIndex));

      this.clickDisabled = !marker.isClickable();
      this.active = marker.isInfoWindowShown();
    }

    @Override
    public void remove() {
      if(marker != null) {
        marker.destroy();
      }
      marker = null;
    }

    void setIconColor(String icon) {
      if(marker != null && COLORS.containsKey(icon)) {
        bitmapDescriptor = BitmapDescriptorFactory.defaultMarker(COLORS.get(icon));
        marker.setIcon(bitmapDescriptor);
      }
    }

    public void  updateIcon() {
        if(marker != null && icon != null && icon.getWidth() != 0 && icon.getHeight() != 0) {
          Bitmap bitmap = Bitmap.createBitmap(icon.getWidth(), icon.getHeight(), Bitmap.Config.ARGB_8888);
          icon.draw(new Canvas(bitmap));
          bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
          marker.setIcon(bitmapDescriptor);
        }
    }

    public void setImage(final String name) {
      if(marker != null) {
        new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
            int drawable = getContext().getResources().getIdentifier(name, "drawable", getContext().getPackageName());
            bitmapDescriptor = BitmapDescriptorFactory.fromResource(drawable);
            if(marker != null) {
              marker.setIcon(bitmapDescriptor);
            }
          }
        }, 0);
      }
    }

    void setAnchor(Double x, Double y) {
        anchorU = x.floatValue();
        anchorV = y.floatValue();
        if(marker != null) {
          marker.setAnchor(anchorU, anchorV);
        }
    }

    void lockToScreen(ReadableArray args) {
        if (args != null) {
            int x = AMapUtils.toPx((float)args.getDouble(0));
            int y = AMapUtils.toPx((float)args.getDouble(1));
            marker.setPositionByPixels(x, y);
        }
    }
}
